﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Managers;
using System.Windows.Input;
using MyAdressBookWPF.Helpers;
using System.Windows;
using System.Windows.Controls;

namespace MyAdressBookWPF.ViewModel
{
    class LoginViewModel : ViewModelBase
    {
        private MainWindowViewModel _window;
        UserDataManager _userDataManager;
        private string _login;
        private ICommand _loginCommand;
        private ICommand _registerCommand;

        public LoginViewModel(MainWindowViewModel window)
        {
            _window = window;
        }

        public string Login 
        {
            get { return _login; }
            set { _login = value; OnPropertyChanged("Login"); }
        }

        #region Commands

        public ICommand LoginCommand
        {
            get
            {
                if (_loginCommand == null)
                {
                    _loginCommand = new Command(ExecuteLoginCommand);
                }
                return _loginCommand;
            }
            set
            {
                _loginCommand = value;
            }
        }

        public ICommand RegisterCommand
        {
            get
            {
                if (_registerCommand == null)
                {
                    _registerCommand = new Command(ExecuteRegisterCommand);
                }
                return _registerCommand;
            }
            set
            {
                _registerCommand = value;
            }
        }
        #endregion

        public void ExecuteLoginCommand(Object parameter)
        {
            var passwordBox = parameter as PasswordBox;
            var password = passwordBox.Password;
            

            if (UserManager.CheckUserConnexion(Login, password, out _userDataManager))
            {
                _window.CurrentViewModel = new ContactViewModel(_userDataManager);
            }
            else
            {
                MessageBox.Show("Login or Password incorrect");
                //_window.CurrentViewModel = this;
            }
        }

        public void ExecuteRegisterCommand()
        {
            _window.CurrentViewModel = new UserRegisterViewModel(_window);
        }
    }
}
